﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="IValidate.WebTester.Views.Home.Index" %>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<form method="post" action="<%= Url.Action("Save") %>">
	<table>
		<tr>
			<td><label for="FirstName">First Name :</label></td>
			<td><%= Html.ValidateTextBox("FirstName") %></td>
		</tr>
		<tr>
			<td><label for="LastName">Last Name :</label></td>
			<td><%= Html.ValidateTextBox("LastName") %></td>
		</tr>
		<tr>
			<td><label for="Birthdate">Birthdate :</label></td>
			<td><%= Html.ValidateTextBox("Birthdate") %></td>
		</tr>
		<tr>
			<td><label for="Email">Email :</label></td>
			<td><%= Html.ValidateTextBox("Email") %></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<input type="submit" onclick="return save()" value="Save" />
			</td>
		</tr>
	</table>
</form>
<script>	
	function save() {
		try {
			$('.validate').each(function() {
				var message = $(this).attr("message");
				var minlength = $(this).attr("minlength");
				var value = $(this).val();
				if (minlength != undefined) {
					if ($(this).val().length < parseInt(minlength)) {
						alert(message);
						return false;
					}
				}
				if ($(this).hasClass('email')) {
					var emailregex = new RegExp('^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$');
					if (!emailregex.test($(this).val())) {
						alert(message);
						return false;
					}
				}
				if ($(this).hasClass('date')) {
					var dateregex = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);
					if (!dateregex.test(value)) {
						alert(message);
						return false;
					}
				}
			});
		}
		catch (ex) {
			console.log(ex);
		}
		return false;
	}
</script>
</asp:Content>
